package com.jxc.summer.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jxc.summer.dao.AccountMapper;
import com.jxc.summer.domain.Account;

@Service
public class LoginService {

	@Autowired
	private AccountMapper accountMapper;
	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED, readOnly=true)
	public Account login(String username,String password) throws Exception{
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("username", username);
		param.put("password", password);
		Account account = accountMapper.findByLogin(param);
		return account;
	}
	
	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED)
	public int updateAccount(Account account){
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("ta_id", account.getAccId());
		param.put("ta_lastlogintime", new Date());
		param.put("ta_lastloginip", account.getAccLastLoginIp());
		param.put("ta_logincount", account.getAccLoginCount()+1);
		param.put("ta_errorpasswordtime", 0);
		int result = accountMapper.updateAccountState(param);
		return result;
	}
	
	public String getAccountCompanyCode(Map<String,Object> params){
		return accountMapper.getAccountCompanyCode(params);
	}
}
